/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Destination;
import edu.chl.da.ejb.ctrl.IManageAirport;
import edu.chl.da.ejb.ctrl.IManageDestination;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

/**
 * Controller bean for add airport events.
 * @author Erik
 */
@ManagedBean(name = "AddAirportController")
@RequestScoped
public class AddAirportController {

    private Integer selectedDestination;

    private String name;
    private String errorMsg;
    private boolean success;

    @EJB
    IManageAirport ima;

    @EJB
    IManageDestination imd;
    

    /** Creates a new instance of AddAirportBean */
    public AddAirportController() {
        errorMsg = null;
    }

    public void addAirport(ActionEvent e)
            throws AbortProcessingException {
        if(name == null ||
           selectedDestination == null ||
           ("").equals(name)){
            this.success = false;
            this.errorMsg = "Alla falt maste vara ifyllda.";
        }
        else{
            this.success = true;
            List<Destination> tmpDestinationList = imd.getAllDestinations();
            Destination dest = tmpDestinationList.get(selectedDestination);
            this.ima.createAirport(name, dest);
        }
    }

    public String navigate(){
        if(success){
            return "/admin/manageAirports.jsf";
        }
        else{
            FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(errorMsg));
            return null; //Same page;
        }
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public Integer getSelectedDestination() {
        return selectedDestination;
    }

    public void setSelectedDestination(Integer selectedDestination) {
        this.selectedDestination = selectedDestination;
    }
}
